# encoding: utf-8
"""
!/usr/bin/python3
@Author: Gao Shuo
@Time: 2018/9/13 10:41 
@ReadMe:把图片中大于1的颜色换成0, 1
    Input: 原始图片文件夹、输出图片文件夹、宽度、高度
    Output: 灰度图片
"""
import os.path
import sys

from PIL import Image

if __name__ == "__main__":
    path = sys.argv[1]
    dest = sys.argv[2]
    width = int(sys.argv[3])
    height = int(sys.argv[4])
    li = os.listdir(path)
    for filepath in li:
        filein = os.path.join(path, filepath)
        print filein
        im = Image.open(filein)
        size = im.size
        image_gray = Image.new("L", size)
        # res = np.zeros((width, height ))
        for i in range(width):
            for j in range(height):
                color =  im.getpixel(xy = (i,j))
                print color
                if int(color) ==1 :
                    image_gray.putpixel((i, j), 255)
                else:
                    image_gray.putpixel((i, j), color)
        image_gray.save(os.path.join(dest, filepath))
        print 'finished: ', filepath

